package com.bjfu.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.R;
import com.bjfu.educms.entity.CrmBanner;
import com.bjfu.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Guli
 * @since 2020-07-11
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    @PostMapping("pageBanner/{page}/{limit}")
    public R PageBanner(
            @PathVariable(value = "page") long page,
            @PathVariable(value = "limit") long limit,
            @RequestBody(required = false) CrmBanner crmBanner
    ){
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        if(crmBanner!= null && !StringUtils.isEmpty(crmBanner.getTitle())){
            wrapper.like("title",crmBanner.getTitle());
        }
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        bannerService.page(bannerPage,wrapper);
        List<CrmBanner> rows = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        return R.ok().data("rows",rows).data("total",total);
    }

    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean success = bannerService.save(crmBanner);
        if(success){
            return R.ok();
        }else {
            return R.error().message("添加轮播图失败");
        }
    }

    @GetMapping("getBanner/{id}")
    public R getBanner(@PathVariable(value = "id") String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return R.ok();
    }

    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable(value = "id") String id){
        bannerService.removeById(id);
        return R.ok();
    }



}

